<?php
/**
 * Utility class for accessing and manipulating the file system
 *
 * @package Concerto
 * @subpackage Utilities
 * @author Jason Raede <jason@torchdm.com>
 */
namespace application\utilities;

class Filesystem {
	
	/**
	 * Get an array of all files in a directory
	 *
	 * @param string $dir The full path to the directory
	 * @param array $fileTypes Optional array of file types to limit the returned files
	 * @return array
	 */
	public static function getFilesInDirectory($dir, $fileTypes = null) {
		$listDir = array(); 
	    if($handler = opendir($dir)) { 
	        while (($sub = readdir($handler)) !== FALSE) { 
	            if ($sub != "." && $sub != ".." && $sub != "Thumb.db") { 
	                if(is_file($dir."/".$sub)) {
	                	if((is_array($fileTypes) && in_array(self::getFileExtension($sub), $fileTypes))) {
	                   		$listDir[] = $sub; 
	                   	}
	                   	elseif(!is_array($file_types)) {
	                   		$listDir[] = $sub;
	                   	}
	                }
	                elseif(is_dir($dir."/".$sub)){ 
	                    $listDir[$sub] = self::getFilesInDirectory($dir."/".$sub, $fileTypes); 
	                } 
	            } 
	        }    
	        closedir($handler); 
	    } 
	    return $listDir; 
	}
	
	/**
	 * Get the extension of a file
	 *
	 * @param string $fileName
	 * @return string
	 */
	public static function getFileExtension($fileName) {
		$dot = Utility::strposall($fileName, '.');
		$count = count($dot);
		$pos = $dot[$count - 1];
		return substr($fileName, $pos + 1);
	}

	/**
	 * Delete all files in a folder, including all sub folders
	 * 
	 * @param string $folder The source folder
	 */
	public static function recursiveDelete($folder){
		if(is_file($folder)){
			return unlink($folder);
		}
		elseif(is_dir$folder){
			if ($handle = opendir($folder)) {
				while (false !== ($file = readdir($handle))) {
					if($file != '.' && $file != '..') self::recursiveDelete($folder.'/'.$file);
	    		}
	    	}
			return rmdir($folder);
		}
	}
	
	/**
	 * Copy all files in a directory
	 *
	 * @param string $src The source directory
	 * @param string $dst The destination directory
	 */
	public static function recursiveCopy($src,$dst) { 
	    $dir = opendir($src); 
	    @mkdir($dst); 
	    while(false !== ( $file = readdir($dir)) ) { 
	        if (( $file != '.' ) && ( $file != '..' )) { 
	            if ( is_dir($src . '/' . $file) ) { 
	                self::recursiveCopy($src . '/' . $file,$dst . '/' . $file); 
	            } 
	            else { 
	                copy($src . '/' . $file,$dst . '/' . $file); 
	            } 
	        } 
	    } 
	    closedir($dir); 
	}

	/**
	 * Unzips a file
	 *
	 * @param string $file The path of the zip file
	 * @param string $dest The folder to unzip it into
	 */
	public static function unzipFile($file, $dest) {
	 
		$path_parts = pathinfo($file);
		$ext = $path_parts['extension'];
	 
		if($ext == 'gz') {
			$execute = "gunzip -".$dest." $file";
			exec($execute);
		}
		if($ext == 'zip') {
			$execute = "unzip -u $file -d $dest";
			exec($execute);
		}
	}
}
